package com.taiji.web.controller.workOrder;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.taiji.common.core.domain.R;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.core.io.ByteArrayResource;
import org.springframework.core.io.FileSystemResource;
import org.springframework.core.io.Resource;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.taiji.common.annotation.Log;
import com.taiji.common.core.controller.BaseController;
import com.taiji.common.core.domain.AjaxResult;
import com.taiji.common.enums.BusinessType;
import com.taiji.workOrder.domain.Project;
import com.taiji.workOrder.service.IProjectService;
import com.taiji.common.utils.poi.ExcelUtil;
import com.taiji.common.core.page.TableDataInfo;

/**
 * 项目信息Controller
 * 
 * @author taiji
 * @date 2023-02-26
 */
@Api(tags="项目管理")
@RestController
@RequestMapping("/workOrder/project")
public class ProjectController extends BaseController
{
    @Autowired
    private IProjectService projectService;


    /**
     * 查询项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:project:list')")
    @GetMapping("/list")
    @ApiOperation("查询项目信息列表")
    public TableDataInfo list(Project project)
    {
        startPage();
        List<Project> list = projectService.selectProjectList(project);
        return getDataTable(list);
    }

    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:project:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Project project)
    {
        List<Project> list = projectService.selectProjectList(project);
        ExcelUtil<Project> util = new ExcelUtil<Project>(Project.class);
        util.exportExcel(response, list, "项目信息数据");
    }

    //projectExport
    /**
     * 导出项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('workOrder:project:export')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @PostMapping("/projectExport/{projectCode}")
    @ApiOperation("导出项目")
    public void projectExport(HttpServletResponse response,@PathVariable String projectCode) throws IOException {

         projectService.projectExport(projectCode,response);
    }

    @GetMapping("/download")
    public ResponseEntity<Resource> downloadFile(Project project) throws IOException {
        String filePath = project.getSearchValue();

        Path path = Paths.get(filePath);
        ByteArrayResource resource = new ByteArrayResource(Files.readAllBytes(path));

        return ResponseEntity.ok()
                .header(HttpHeaders.CONTENT_DISPOSITION, "attachment;filename=" + path.getFileName().toString())
                .contentType(MediaType.APPLICATION_OCTET_STREAM)
                .contentLength(resource.contentLength())
                .body(resource);
    }

    /**
     * 获取项目信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:project:query')")
    @GetMapping(value = "/{id}")
    @ApiOperation("获取项目信息详细信息")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(projectService.selectProjectById(id));
    }

    /**
     * 新增项目信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:project:add')")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    @ApiOperation("新增项目信息")
    public AjaxResult add(@RequestBody Project project) throws Exception {
        return toAjax(projectService.insertProject(project));
    }

    /**
     * 修改项目信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:project:edit')")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    @ApiOperation("修改项目信息")
    public AjaxResult edit(@RequestBody Project project)
    {
        return toAjax(projectService.updateProject(project));
    }

    /**
     * 删除项目信息
     */
    @PreAuthorize("@ss.hasPermi('workOrder:project:remove')")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    @ApiOperation("删除项目信息")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(projectService.deleteProjectByIds(ids));
    }

    /**
     * 查询当前用户的项目列表
     */
    @GetMapping("/getProjectList")
    @ApiOperation("查询当前用户的项目列表")
    public R getProjectList(@RequestHeader(value = "Authorization")String token) throws Exception {
        return R.ok(projectService.getProjectList(token));
    }
}
